package com.gangquan360.smartadmin.module.product.service;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.constant.ResponseCodeConst;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.product.constant.ProductResponseCodeConst;
import com.gangquan360.smartadmin.module.product.dao.ProductCategoryDao;
import com.gangquan360.smartadmin.module.product.domain.dto.ProductCategoryDTO;
import com.gangquan360.smartadmin.module.product.domain.dto.ProductCategoryQueryDTO;
import com.gangquan360.smartadmin.module.product.domain.dto.ProductCategoryVO;
import com.gangquan360.smartadmin.module.product.domain.entity.ProductCategoryEntity;
import com.gangquan360.smartadmin.util.SmartBeanUtil;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;
import com.gangquan360.smartadmin.util.SmartTreeUtil;
import com.google.common.collect.Lists;

import lombok.extern.slf4j.Slf4j;

/**
 * [ 产品分类表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-14 21:32:22
 * @since JDK1.8
 */
@Service
@Slf4j
public class ProductCategoryService {

    @Autowired
    private ProductCategoryDao productCategoryDao;


    /**
     * @author wiggin
     * @description 分页查询
     * @date 2020-03-14 21:32:22
     */
    public ResponseDTO<PageResultDTO<ProductCategoryDTO>> queryByPage(ProductCategoryQueryDTO queryDTO) {
        Page<ProductCategoryDTO> page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<ProductCategoryEntity> entities = productCategoryDao.queryByPage(page, queryDTO);
        List<ProductCategoryDTO> dtoList = SmartBeanUtil.copyList(entities, ProductCategoryDTO.class);
        page.setRecords(dtoList);
        PageResultDTO<ProductCategoryDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }
    
    
    public ResponseDTO<List<ProductCategoryVO>> getTree() {
    	Wrapper<ProductCategoryEntity> wrapper = new EntityWrapper<>();
		List<ProductCategoryEntity> selectList = productCategoryDao.selectList(wrapper);
		List<ProductCategoryVO> categoryTree = SmartTreeUtil.createTree(ProductCategoryVO.class, selectList, Long.valueOf(0), ProductCategoryEntity::getParentId, cate -> (cate.getId()) , (ProductCategoryVO productCategoryVo, List<ProductCategoryVO> productCategoryVoList) -> {
			productCategoryVo.setChildren(productCategoryVoList);
        });
    	return ResponseDTO.succData(categoryTree);
    }
    

    /**
     * @author wiggin
     * @description 添加
     * @date 2020-03-14 21:32:22
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(ProductCategoryDTO addDTO) {
    	ProductCategoryEntity productCategoryEntity = new ProductCategoryEntity();
    	productCategoryEntity.setCategory(addDTO.getCategory());
    	Wrapper<ProductCategoryEntity> wrapper = new EntityWrapper<>(productCategoryEntity);
		// 判断名称
    	Integer selectCount = productCategoryDao.selectCount(wrapper);
    	if (selectCount != null && selectCount.compareTo(0) > 0) {
    		return ResponseDTO.wrap(ProductResponseCodeConst.CATENAME_REPEAT);
    	}
        ProductCategoryEntity entity = SmartBeanUtil.copy(addDTO, ProductCategoryEntity.class);
        // 获取所有的分类树id
        Long parentId = addDTO.getParentId();
        if (parentId != null && !parentId.equals(0L)) {
        	ProductCategoryEntity parentCateEntity = productCategoryDao.selectById(parentId);
        	if (parentCateEntity.getPath() != null) {
        		entity.setPath(parentCateEntity.getPath() + "," + parentId);
        	}
        }
        productCategoryDao.insert(entity);
        return ResponseDTO.succ();
    }
    
 
    /**
     * 
     * @param categoryId
     * @return
     */
    @SuppressWarnings("unused")
	private List<Long> getCategoryParentIds(Long categoryId) {
    	List<Long> ids = Lists.newArrayList();
    	ProductCategoryEntity entity = productCategoryDao.selectById(categoryId);
    	if (entity == null) {
    		return ids;
    	}
    	if (entity.getParentId().equals(0L)) {
    		ids.add(categoryId);
    		return ids;
    	}
    	List<Long> categoryParentId = getCategoryParentIds(entity.getId());
    	ids.addAll(categoryParentId);
    	return ids;
    }
    


    /**
     * @author wiggin
     * @description 编辑
     * @date 2020-03-14 21:32:22
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(ProductCategoryDTO updateDTO) {
    	ProductCategoryEntity productCategoryEntity = new ProductCategoryEntity();
    	productCategoryEntity.setCategory(updateDTO.getCategory());
    	Wrapper<ProductCategoryEntity> wrapper = new EntityWrapper<>(productCategoryEntity);
    	wrapper.ne("id", updateDTO.getId());
		// 判断名称
    	Integer selectCount = productCategoryDao.selectCount(wrapper);
    	if (selectCount != null && selectCount.compareTo(0) > 0) {
    		return ResponseDTO.wrap(ProductResponseCodeConst.CATENAME_REPEAT);
    	}
        ProductCategoryEntity entity = SmartBeanUtil.copy(updateDTO, ProductCategoryEntity.class);
        productCategoryDao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 删除
     * @date 2020-03-14 21:32:22
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
    	ProductCategoryEntity entity = new ProductCategoryEntity();
    	entity.setParentId(id);
		Wrapper<ProductCategoryEntity> wrapper = new EntityWrapper<>(entity);
		Integer selectCount = productCategoryDao.selectCount(wrapper);
		if (selectCount != null && selectCount > 0) {
			return ResponseDTO.wrap(ProductResponseCodeConst.CANNOT_DEL_CATEGORY_WITH_CHILD);
		}
        productCategoryDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author wiggin
     * @description  根据ID查询
     * @date 2020-03-14 21:32:22
    */
    public ResponseDTO<ProductCategoryDTO> detail(Long id){
        ProductCategoryEntity entity = productCategoryDao.selectById(id);
        ProductCategoryDTO dto =  SmartBeanUtil.copy(entity,ProductCategoryDTO.class);
        return ResponseDTO.succData(dto);
    }
}
